<?php
namespace Lib\Source\Driver;
use Lib\Data;
use Lib\Source\DriverAbstract;
class Memcache extends DriverAbstract{
    /**
     * @var Data
     */
    protected $_storage;
    
    /**
     * @var \Memcache
     */
    protected $_memcache;

    /**
     * @todo Need to set connection options
     */
    public function init(){
        $this->_memcache = new \Memcache();
        $this->_memcache->connect('localhost', 11211);
    }
    
	/**
     * @see Lib\Source.DriverAbstract::load()
     */
    public function load($name, $default = null){
        $loadedData = $this->_memcache->get($name);
        
        if($loadedData){
            $this->getStorage()->setValue($name, $loadedData);
            return true;
        } else{
            $this->getStorage()->setValue($name, $default);
            return false;
        }
    }
    
    
    /**
     *  @see Lib\Source.DriverAbstract::saveData()
     */
    public  function saveData($data){
        $result = false;
        foreach ($data as $name=>$value){
//            if(is_array($value) || is_object($value)) continue; // Filter unneeded data from cookie
            $this->_memcache->set($name, $value, $this->getOption('lifetime', 120));
            $result = true;
        }       
        return $result;
    }
}